package cn.cai.pethome.dept.service;


import cn.cai.pethome.PetHomeApp;
import cn.cai.pethome.base.dto.LogininfoDto;
import cn.cai.pethome.base.util.JwtUtils;
import cn.cai.pethome.base.util.Payload;
import cn.cai.pethome.base.util.RsaUtils;
import cn.cai.pethome.emp.domain.Employee;
import cn.cai.pethome.logininfo.domain.Logininfo;
import cn.cai.pethome.system.domain.Menu;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;



public class LogininfoDtoText {


    @Value("${jwt.privateKey}")
    private String privateKey;


    @Test
    public void testCreateJWT() throws Exception {
        //1.利用私钥加密  待会要用公钥来解密
        PrivateKey privateKey = RsaUtils.getPrivateKey(JwtUtils.class.getClassLoader().getResource(this.privateKey).getFile());

        //2.载荷数据：登录成功后的Logininfo对象作为载荷
        Logininfo logininfo = new Logininfo();
        logininfo.setId(123L);
        logininfo.setUsername("小菜");
        logininfo.setEmail("2567209139@qq.com");
        logininfo.setPhone("15364463790");

        Employee admin = new Employee();
        admin.setUsername("菜菜");
        admin.setShop_id(20L);

        List<Menu> ownMenus = new ArrayList<>();
        List<String> ownPermissions = new ArrayList<>();

        //封装数据
        LogininfoDto logininfoDto = new LogininfoDto(logininfo,admin,ownMenus,ownPermissions);


        //3.加密
        String token = JwtUtils.generateTokenExpireInMinutes(logininfoDto, privateKey, 30);
        System.out.println(token);
    }

    @Value("${jwt.publicKey}")
    private String publicKey;

    @Test
    public void testParseJWT() throws Exception {
        String jwtToken = "eyJhbGciOiJSUzI1NiJ9.eyJ1c2VyIjoie1wiYWRtaW5cIjp7XCJzaG9wSWRcIjoyNCxcInN0YXRlXCI6MCxcInVzZXJuYW1lXCI6XCJhZG1pbjEwMVwifSxcImxvZ2luaW5mb1wiOntcImRpc2FibGVcIjpmYWxzZSxcImVtYWlsXCI6XCI5NTc4OTg3QHFxLmNvbVwiLFwiaWRcIjoxMjMsXCJwaG9uZVwiOlwiMTg5OTYxNTczMDBcIixcInR5cGVcIjowLFwidXNlcm5hbWVcIjpcImFkbWluMTAxXCJ9LFwib3duTWVudXNcIjpbXSxcIm93blBlcm1pc3Npb25zXCI6W119IiwianRpIjoiTW1JelpUVTFNMkV0TW1NNU1TMDBOamxoTFRrME16WXRNekF5WlROaFkyWXlZVFl3IiwiZXhwIjoxNjYyNjIwMzA5fQ.Ku-yiLQw0imfuzBOVTJqeiw1ce03Pu19tjmOVkkcTo-q7djfHNs7UhvWclmjkBOr1HFFG5m39lD7IiMGzVpogLNk92LRF5r734Oun4Pr-SbsdKzzVN0y55NaAOv0eTSeTPr8qrE6QglWR-VHGDGACE2ZArupyPgEusjywd3jvMDMAIXfjBMSSimMkRNPiu6uoAZqglp28_7vqxymuAV-fIwyU1HBfzJ8u2PnhgWN_FLGBTHwt6f7sL0wowr8zSb4GOzh_8IkXf4KL_pdfN7PPykAb-_oZvx9IUcX7WfpuRykUJAFp5mvhUellApw5PIFJP8U0WZ3SmAj1CU63ny-0Q";

        // 1.利用公钥解密
        PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource(this.publicKey).getFile());
        Payload<LogininfoDto> payload = JwtUtils.getInfoFromToken(jwtToken, publicKey, LogininfoDto.class);

        //2.从载荷身上获取数据
        LogininfoDto logininfoDto = payload.getUserInfo();
        System.out.println(logininfoDto);

    }
}
